﻿namespace DataModel
{
    public class K8055Data
    {
        private int _output;
        private int _input;

        public int Output
        {
            get { return _output; }
            set { _output = value; }
        }

        public int Input
        {
            get { return _input; }
            private set { _input = value; }
        }

        private static bool GetBit(int data, short index)
        {
            int mask = 1 << index;
            return (data & mask) != 0;
        }

        private static void SetBit(ref int data, short index, bool value)
        {
            if (value)
            {
                data = data | (1 << index);
            }
            else
            {
                data = data & ~(1 << index);
            }
        }

        private static byte GetByte(int data, short index)
        {
            int mask = 0xFF << (index * 8);
            return (byte)((data & mask) >> (index * 8));
        }

        private static void SetByte(ref int data, short index, byte value)
        {
            int mask = ~(0xFF << (index * 8));
            data = data & mask;
            mask = value << (index * 8);
            data = data | mask;
        }

        public bool OutputPin1 { get { return GetBit(_output, 0); } set { SetBit(ref _output, 0, value); } }

        public bool OutputPin2 { get { return GetBit(_output, 1); } set { SetBit(ref _output, 1, value); } }

        public bool OutputPin3 { get { return GetBit(_output, 2); } set { SetBit(ref _output, 2, value); } }

        public bool OutputPin4 { get { return GetBit(_output, 3); } set { SetBit(ref _output, 3, value); } }

        public bool OutputPin5 { get { return GetBit(_output, 4); } set { SetBit(ref _output, 4, value); } }

        public bool OutputPin6 { get { return GetBit(_output, 5); } set { SetBit(ref _output, 5, value); } }

        public bool OutputPin7 { get { return GetBit(_output, 6); } set { SetBit(ref _output, 6, value); } }

        public bool OutputPin8 { get { return GetBit(_output, 7); } set { SetBit(ref _output, 7, value); } }

        public byte OutputByte0 { get { return GetByte(_output, 0); } set { SetByte(ref _output, 0, value); } }

        public byte OutputByte1 { get { return GetByte(_output, 1); } set { SetByte(ref _output, 1, value); } }

        public byte OutputByte2 { get { return GetByte(_output, 2); } set { SetByte(ref _output, 2, value); } }

        public bool InputPin1 { get { return GetBit(_input, 0); } set { SetBit(ref _input, 0, value); } }

        public bool InputPin2 { get { return GetBit(_input, 1); } set { SetBit(ref _input, 1, value); } }

        public bool InputPin3 { get { return GetBit(_input, 2); } set { SetBit(ref _input, 2, value); } }

        public bool InputPin4 { get { return GetBit(_input, 3); } set { SetBit(ref _input, 3, value); } }

        public bool InputPin5 { get { return GetBit(_input, 4); } set { SetBit(ref _input, 4, value); } }

        public byte InputByte0 { get { return GetByte(_input, 0); } set { SetByte(ref _input, 0, value); } }

        public byte InputByte1 { get { return GetByte(_input, 1); } set { SetByte(ref _input, 1, value); } }

        public byte InputByte2 { get { return GetByte(_input, 2); } set { SetByte(ref _input, 2, value); } }
    }
}